#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Taotie Ren
# import logging 
# 禁用日志模块
# logging.disable(logging.CRITICAL)
# logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')

# formFiller.py - Automatically fills in the form
import pyautogui, time

# Set these to the correct coordinates for your computer.
nameField = (648, 319)
submitButton = (651, 817)
submitButtonColor = (75, 141, 249)
submitAnotherLink = (760, 224)

formData = [{'name': 'Alice', 'fear': 'Eavesdroppers', 'source': 'wand',
             'robocap': 4, 'comments': 'Tell Bob I said hi.'},
            {'name': 'Bob', 'fear': 'bees', 'source': 'amulet',
             'robocap': 4, 'comments': 'n/a.'},
            {'name': 'Carol', 'fear': 'puppets', 'source': 'crystal ball',
             'robocap': 1, 'comments': 'Please take the puppets out of the braek room.'},
            {'name': 'Alice murphy', 'fear': 'ED-209', 'source': 'money',
             'robocap': 5, 'comments': 'Protect the innocent. Sarve the public trust. Uphold the law.'},
            ]

pyautogui.PAUSE = 0.5

for person in formData:
	# Give the user a chance to kill the script.
	print('>>> 5 SECOND PAUSE TO LET USER PRESS CTRL-C <<<')
	time.sleep(5)

	# Wait until the form page has loaded.
	while not pyautogui.pixelMatchesColor(submitButton[0], submitButton[1],submitButtonColor):
		time.sleep(5)

	print('Entering %s info ...' % (person['name']))
	pyautogui.click(nameField[0], nameField[1])
	# Fill out the Name Field.
	pyautogui.typewrite(person['name'] + '\t')

	# Fill out the Greatest Fear(s) field.
	pyautogui.typewrite(person['fear'] + '\t')

	# Fill out the Source of Wizard Powers field.
	if person['source'] == 'wand':
		pyautogui.typewrite(['down', '\t'])
	elif person['source'] == 'amulet':
		pyautogui.typewrite(['down', 'down', '\t'])
	elif person['source'] == 'crystal ball':
		pyautogui.typewrite(['down', 'down', 'down', '\t'])
	elif person['source'] == 'monkey':
		pyautogui.typewrite(['down', 'down', 'down', 'down', '\t'])

	# Fill out the RoboCop Field.
	if person['robocop'] == 1:
		pyautogui.typewrite([' ', '\t'])
	elif person['robocop'] == 2:
		pyautogui.typewrite(['right', '\t'])
	elif person['robocop'] == 3:
		pyautogui.typewrite(['right', 'right', '\t'])
	elif person['robocop'] == 4:
		pyautogui.typewrite(['right', 'right', 'right', '\t'])
	elif person['robocop'] == 5:
		pyautogui.typewrite(['right', 'right', 'right', 'right',  '\t'])

	# Fill out the addittional Comments field.
	pyautogui.typewrite(person['comments'] + '\t')

	# Click Submit.
	pyautogui.press('enter')

	# Wait until form page has loaded.
	print('Clicked Submit.')
	time.sleep(5)

	# click the Submit another response link.
	pyautogui.click(submitAnotherLink[0], submitAnotherLink[1])